package com.loadmore.common.auth

import android.os.Bundle
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.loadmore.common.App
import com.loadmore.common.R
import com.loadmore.common.base.BaseActivity
import com.loadmore.common.constants.Api
import com.loadmore.common.constants.Configs
import com.loadmore.common.constants.Constants
import com.loadmore.common.model.BaseResponse
import com.loadmore.common.model.Token
import com.loadmore.common.model.User
import com.loadmore.common.net.Request
import com.loadmore.common.net.impl.JsonCallback
import com.loadmore.common.router.RouterPath
import com.loadmore.common.util.SPFUtil
import com.loadmore.common.variables.Variables
import com.lzy.okgo.model.Response
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog.Builder.ICON_TYPE_LOADING
import kotlinx.android.synthetic.main.activity_login.*

@Route(path = RouterPath.Login)
class LoginActivity : BaseActivity() {

    private var logining = false
    var dialog:QMUITipDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        hideNavigationIcon()
       // actionBar.setCenterTitle("登录")
        dialog = QMUITipDialog.Builder(this).setIconType(ICON_TYPE_LOADING).setTipWord("正在登陆...").create(false)
        login.setOnClickListener {
            logining = true
            dialog?.show()
            val params:HashMap<String,Any> = hashMapOf("email" to email.text.toString(),
                    "password" to password.text.toString())
            Request.post(Api.login, params,object : JsonCallback<BaseResponse<Token>>(){
                override fun onSuccess(response: Response<BaseResponse<Token>>?) {
                    var data = response?.body()
                    var token =  data?.data?.access_token
                    if (token != null) {
                        Variables.token = token
                        getUserInfo(token)
                    }else {
                        loginFail()
                    }
                }

                override fun onError(response: Response<BaseResponse<Token>>?) {
                    super.onError(response)
                    loginFail()
                }

            })
        }

        register.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Register).navigation()
        }
    }

    public fun loginFail() {

        logining = false
        dialog?.hide()
        Toast.makeText(this@LoginActivity,"登录失败！",Toast.LENGTH_SHORT).show()
    }

    public fun getUserInfo(token:String) {
        val params:HashMap<String,Any> = hashMapOf("token" to token)
        Request.post(Api.info, params, object : JsonCallback<BaseResponse<User>>() {
            override fun onSuccess(response: Response<BaseResponse<User>>?) {
                    var data = response?.body()
                    var user = data?.data
                    logining = false
                    if (user != null && data?.code == 200 && data.data != null) {
                        App.setDataBase(applicationContext, user!!.id!!)
                        App.daoSession.insertOrReplace(user)
                        Variables.user = user
                        Variables.token = token
                        SPFUtil(Configs.User).putString(Constants.token,token)
                        SPFUtil(Configs.User).putLong(Constants.user_id,user.id)
                        ARouter.getInstance().build(RouterPath.Main).navigation()
                        dialog?.hide()
                        finish()
                    }
            }

        })
    }

    override fun onBackPressed() {
        super.onBackPressed()
    }
}
